﻿using project.B12.Model.Enums;
using software.Common.MVVM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace project.B12.Model.Models
{

    public class MonitorMes : ObservableObject
    {
        private HoleState _state;
        private int _shelf;
        private int _index;
        private string _plateID;
        private string _plateType;
        private string _sampleID;
        private string _sampleType;
        private DateTime? _loadTime;
        private DateTime? _addSampleTime;
        private DateTime? _photoTime;
        private int _LorF;
        private int _addSampleCount;
        private int _cultureTime;
        private string _countdown;
        public System.Threading.Timer? mytimer;
        public MonitorMes(int shelf ,int index)
        {
            _shelf = shelf;
            _index = index;
        }

        #region 属性
        public HoleState State
        {
            get => _state;
            set
            {
                _state = value;
                this.RaisePropertyChanged();
                if (_state == HoleState.Incubation)
                {
                    mytimer = new System.Threading.Timer(new System.Threading.TimerCallback(IncubationTiming), null, 0, 1000);
                    mytimer.Change(0, 1000);
                }
            }
        }
        public int Shelf
        {
            get => _shelf;
            set => SetProperty(ref _shelf, value);
        }
        public int Index
        {
            get => _index;
            set => SetProperty(ref _index, value);
        }
        public string PlateID
        {
            get => _plateID;
            set => SetProperty(ref _plateID, value);
        }

        /// <summary>
        /// 药敏板项目名
        /// </summary>
        public string PlateType
        {
            get => _plateType;
            set => SetProperty(ref _plateType, value);
        }

        public string? SampleID
        {
            get => _sampleID;
            set => SetProperty(ref _sampleID, value);
        }

        /// <summary>
        /// 菌名称
        /// </summary>
        public string SampleType
        {
            get => _sampleType;
            set => SetProperty(ref _sampleType, value);
        }

        public DateTime? LoadTime
        {
            get => _loadTime;
            set => SetProperty(ref _loadTime, value);
        }
        public DateTime? AddSampleTime
        {
            get => _addSampleTime;
            set => SetProperty(ref _addSampleTime, value);
        }
        public DateTime? PhotoTime
        {
            get => _photoTime;
            set => SetProperty(ref _photoTime, value);
        }

        /// <summary>
        /// 孵育倒计时
        /// </summary>
        public string Countdown
        {
            get => _countdown;
            set => SetProperty(ref _countdown, value);
        }
        public int LorR
        {
            get => _LorF;
            set => SetProperty(ref _LorF, value);
        }
        public int AddSampleCount
        {
            get => _addSampleCount;
            set => SetProperty(ref _addSampleCount, value);
        }

        /// <summary>
        /// 培育时间
        /// </summary>
        public int CultureTime
        {
            get => _cultureTime;
            set => SetProperty(ref _cultureTime, value);
        }

        #endregion

        #region 方法

        /// <summary>
        /// 孵育倒计时
        /// </summary>
        /// <param name="a"></param>
        public void IncubationTiming(object a)
        {
            if (_addSampleTime != null)
            {
                if ((_addSampleTime + new TimeSpan(0, 0, _cultureTime)) > DateTime.Now)
                {
                    Countdown = (_addSampleTime + new TimeSpan(0, 0, _cultureTime) - DateTime.Now).Value.ToString(@"hh\:mm\:ss");
                }
                else
                {
                    mytimer.Dispose();
                    State = HoleState.WaitingForPhotograph;
                    Countdown = string.Empty;
                }
            }
        }

        public void ApplyPlate(string plateID)
        {
            PlateID = plateID;
            PlateType = plateID.Substring(0, 4);
            _cultureTime = 24 * 60 * 60;
            LoadTime = DateTime.Now;
        }
        public void BindingSample(string sampleID, string bacteriumName)
        {
            SampleID = sampleID;
            SampleType = bacteriumName;
        }

        public void GoToAddSample()
        {
            State = HoleState.SAMPLING;
        }
        public void AddSampleComplete()
        {
            AddSampleTime = DateTime.Now;
            State = HoleState.Incubation;
        }
        public void GoToPhoto()
        {
            State = HoleState.Photograph;
        }
        public void PhotoError()
        {
            PhotoTime = DateTime.Now;
            State = HoleState.PhotographError;
        }
        public void PhotoComplete()
        {
            PhotoTime = DateTime.Now;
            State = HoleState.DONE;
        }
        #endregion

    }
}
